package com.peas.platform.module.icims;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peas.platform.module.icims.base.sys.dto.RecycleDto;
import com.peas.platform.module.icims.base.sys.service.RecycleService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RecycleUpdateTestCase {

    @Resource
    private RecycleService recycleService;

    private List<RecycleDto> list(int curr, int size) {
        Page<RecycleDto> pager = new Page<>();
        pager.setCurrent(curr);
        pager.setSize(size);
        Page<RecycleDto> page = recycleService.page(pager);
        return page.getRecords();
    }

    @Test
    public void updateAsync() {
        List<Integer> splitSizes = Arrays.asList(1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 50, 1);
        for (int splitSize : splitSizes) {
            List<RecycleDto> list = list(1, 1000);
            long time = updateAsync(list, splitSize);
            System.out.println("splitSize:" + splitSize + ",used:" + time);
        }
    }

    @Test
    public void update() {
        List<Integer> size = Arrays.asList(1,1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 50);
        List<Long> list1 = new ArrayList<>();
        List<Long> list2 = new ArrayList<>();
        for (int s : size) {
            List<RecycleDto> data1 = list(1, s);
            System.out.println("save1:" + s);
            System.out.println(data1);
//            list1.add(update1(data1));
            List<RecycleDto> data2 = list(2, s);
            System.out.println("save2:" + s);
            System.out.println(data2);
            System.out.println(data2.get(0).getId());
            System.out.println(data2.get(0).getVersion());
            list2.add(update2(data2));
        }
        System.out.println(list1);
        System.out.println(list2);
    }

    private long update1(List<RecycleDto> list) {
        StopWatch sw = new StopWatch();
        sw.start();
        recycleService.updateBatch1(list);
        sw.stop();
        return sw.getLastTaskTimeMillis();
    }

    private long update2(List<RecycleDto> list) {
        StopWatch sw = new StopWatch();
        sw.start();
        recycleService.updateBatch2(list);
        sw.stop();
        return sw.getLastTaskTimeMillis();
    }

    private long updateAsync(List<RecycleDto> list, int splitSize) {
        StopWatch sw = new StopWatch();
        sw.start();
        recycleService.updateBatchAsync1(list, splitSize);
        sw.stop();
        return sw.getLastTaskTimeMillis();
    }

}
